package com.zy.qudadid.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.zy.cityList.CityPickerActivity;
import com.zy.lcpassenger.R;
import com.zy.qudadid.presenter.IndexSelectaddsPresenter;
import com.zy.qudadid.ui.activity.base.RecycleViewNoToolBarActivity;
import com.zy.qudadid.ui.adapter.IndexSelectaddsAdapter;
import com.zy.qudadid.ui.view.IndexSelectaddsView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by ninos on 2016/9/20.
 */
public class IndexSelectaddsActivity extends RecycleViewNoToolBarActivity<IndexSelectaddsPresenter, IndexSelectaddsAdapter, PoiItem> implements IndexSelectaddsView, PoiSearch.OnPoiSearchListener {

    @Bind(R.id.img_back)
    TextView imgBack;
    @Bind(R.id.tv_title)
    EditText tvTitle;
    @Bind(R.id.tv_action)
    TextView tvAction;

//    String state = "in";
    String city="临沂市";
    String cityid="";


    ArrayList<PoiItem> list = new ArrayList<>();

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_index_selectadds;
    }

    @Override
    protected void initThings(Bundle savedInstanceState) {
        super.initThings(savedInstanceState);
//        state = getIntent().getBundleExtra("data").getString("state");
        city = getIntent().getBundleExtra("data").getString("city");
        imgBack.setText(city);
    }

    @Override
    public void initListeners() {

    }

    @Override
    public IndexSelectaddsPresenter createPresenter() {
        return new IndexSelectaddsPresenter();
    }

    @Override
    protected IndexSelectaddsAdapter provideAdapter() {
        return new IndexSelectaddsAdapter(getContext(), presenter);
    }

    @Override
    protected RecyclerView.LayoutManager provideLayoutManager() {
        return new LinearLayoutManager(getContext());
    }

    @Override
    public void onItemClick(View view, int pos, PoiItem item) {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(tvTitle.getWindowToken(), 0);
        Intent data = new Intent();
        data.putExtra("poiitem", item);
        setResult(2010, data);
        finish();
    }

    @OnTextChanged(R.id.tv_title)
    public void searchAddress(){
        if (tvTitle.getText().toString().trim().equals("")){
            return;
        }
        PoiSearch.Query query = new PoiSearch.Query(tvTitle.getText().toString(), "汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施", city);
        //keyWord表示搜索字符串，
        //第二个参数表示POI搜索类型，二者选填其一，
        //POI搜索类型共分为以下20种：汽车服务|汽车销售|
        //汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|
        //住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|
        //金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施
        //cityCode表示POI搜索区域，可以是城市编码也可以是城市名称，也可以传空字符串，空字符串代表全国在全国范围内进行搜索
        query.setPageSize(10);// 设置每页最多返回多少条poiitem
        query.setPageNum(1);//设置查询页码

        PoiSearch poiSearch = new PoiSearch(this, query);
        poiSearch.setOnPoiSearchListener(this);

        poiSearch.searchPOIAsyn();
    }



    @OnClick({R.id.img_back, R.id.tv_action})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
//                if (state.equals("in")){
//                    toast("目前处于市内行程，不能更换城市");
//                }else{
                    startActivityForResult(CityPickerActivity.class,1023);
//                }
                break;
            case R.id.tv_action:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1023&&resultCode == -1){
            city = data.getStringExtra("picked_city");
            cityid = data.getStringExtra("picked_city");
            imgBack.setText(city);
        }
    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        list = poiResult.getPois();
        bd(list);
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }
}
